package com.hongshan.wallet.components

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.widget.EditText
import com.hongshan.wallet.R


/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.components
 * @author   : zhangxx
 * @date     : 2019/3/27
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */
/**
 * Created by Darren on 2016/12/14.
 * Email: 240336124@qq.com
 * Description: 密码输入框
 */

class PassWordEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    EditText(context, attrs) {
    // 画笔
    private val mPaint: Paint by lazy { Paint() }
    // 一个密码所占的宽度
    private var mPasswordItemWidth = 0F
    // 密码的个数默认为6位数
    private val mPasswordNumber = 6
    // 背景边框颜色
    private var mBgColor = Color.parseColor("#d1d2d6")
    // 背景边框大小
    private var mBgSize = 1F
    // 背景边框圆角大小
    private var mBgCorner = 0F
    // 分割线的颜色
    private var mDivisionLineColor = mBgColor
    // 分割线的大小
    private var mDivisionLineSize = 1F
    // 密码圆点的颜色
    private var mPasswordColor = mDivisionLineColor
    // 密码圆点的半径大小
    private var mPasswordRadius = 4F

    init {
        initPaint()
        initAttributeSet(context, attrs)
        // 不显示光标
        background = ColorDrawable(Color.TRANSPARENT)
        isCursorVisible = false
    }

    /**
     * 初始化属性
     */
    private fun initAttributeSet(context: Context, attrs: AttributeSet?) {
        val array = context.obtainStyledAttributes(attrs, R.styleable.PassWordEditText)
        // 获取大小
        mDivisionLineSize = array.getDimension(R.styleable.PassWordEditText_divisionLineSize, dip2px(mDivisionLineSize))
        mPasswordRadius = array.getDimension(R.styleable.PassWordEditText_passwordRadius, dip2px(mPasswordRadius))
        mBgSize = array.getDimension(R.styleable.PassWordEditText_bgSize, dip2px(mBgSize))
        mBgCorner = array.getDimension(R.styleable.PassWordEditText_bgCorner, 0f)
        // 获取颜色
        mBgColor = array.getColor(R.styleable.PassWordEditText_bgColor, mBgColor)
        mDivisionLineColor = array.getColor(R.styleable.PassWordEditText_divisionLineColor, mDivisionLineColor)
        mPasswordColor = array.getColor(R.styleable.PassWordEditText_passwordColor, mDivisionLineColor)
        array.recycle()
    }

    /**
     * 初始化画笔
     */
    private fun initPaint() {
        mPaint.isAntiAlias = true
        mPaint.isDither = true
    }

    /**
     * dip 转 px
     */
    private fun dip2px(dip: Float) =
        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, resources.displayMetrics)

    override fun onDraw(canvas: Canvas) {
        val passwordWidth = width - (mPasswordNumber - 1) * mDivisionLineSize
        mPasswordItemWidth = passwordWidth / mPasswordNumber
        // 绘制背景
        drawBg(canvas)
        // 绘制分割线
        drawDivisionLine(canvas)
        // 绘制密码
        drawHidePassword(canvas)
    }

    /**
     * 绘制背景
     */
    private fun drawBg(canvas: Canvas) {
        mPaint.color = mBgColor
        // 设置画笔为空心
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = mBgSize
        val rectF =
            RectF(mBgSize.toFloat(), mBgSize.toFloat(), (width - mBgSize).toFloat(), (height - mBgSize).toFloat())
        // 如果没有设置圆角，就画矩形
        if (mBgCorner == 0F) {
            canvas.drawRect(rectF, mPaint)
        } else {
            // 如果有设置圆角就画圆矩形
            canvas.drawRoundRect(rectF, mBgCorner, mBgCorner, mPaint)
        }
    }

    /**
     * 绘制隐藏的密码
     */
    private fun drawHidePassword(canvas: Canvas) {
        val passwordLength = text.length
        mPaint.color = mPasswordColor
        // 设置画笔为实心
        mPaint.style = Paint.Style.FILL
        for (i in 0 until passwordLength) {
            val cx = i * mDivisionLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize
            canvas.drawCircle(cx, height / 2F, mPasswordRadius, mPaint)
        }
    }

    /**
     * 绘制分割线
     */
    private fun drawDivisionLine(canvas: Canvas) {
        mPaint.strokeWidth = mDivisionLineSize
        mPaint.color = mDivisionLineColor
        for (i in 0 until mPasswordNumber - 1) {
            val startX = (i + 1) * mDivisionLineSize + (i + 1) * mPasswordItemWidth + mBgSize
            canvas.drawLine(startX, mBgSize, startX, height - mBgSize, mPaint)
        }
    }
}